package com.lottery.controller.pc28.accoutController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lottery.service.pc28.accoutService.AutomaticCustomerService;
import com.lottery.service.pc28.accoutService.ManualCustomerservice;
import com.lottery.service.pc28.accoutService.ServerOnlineService;
import com.lottery.service.pc28.wealthService.QueryAccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

//客服
@Controller
public class CustomerController {

	@Resource
	public QueryAccountService queryAccountService;

	@Autowired
	private ManualCustomerservice manualCustomerservice;

	@Autowired
	private AutomaticCustomerService automaticcustomerservice;

	@Autowired
	private ServerOnlineService serveronlineservice;

	// 日志
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	// 获取客服ID
	@CrossOrigin
	@RequestMapping(value = "/api/customer/public/getCustomerId", produces = {
			"application/json;charset=UTF-8" }, method = RequestMethod.POST)
	@ResponseBody
	public String getCustomerId(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********getCustomerId in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int clientId = obj.getInteger("userId");
			logger.info("getCustomerId clientId:{}", clientId);
			String customerId = serveronlineservice.getCustomerId(clientId);
			logger.info("********getcustomerid out********:" + customerId);
			return customerId;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****getCustomerId error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}
		return jObject.toJSONString();

	}

	// 客服完成
	@CrossOrigin
	@RequestMapping(value = "/api2/finishServiced", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服 端
	@ResponseBody
	public String finishService(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		String body;
		try {
			body = readHttpBody(request);
			// 日志
			logger.info("********finishservice in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int serverId = obj.getInteger("serverId");
			int clientId = obj.getInteger("clientId");
			logger.info("smscode serverId:{},clientId:{}", serverId, clientId);
			String finishService = serveronlineservice.finishService(serverId, clientId);
			logger.info("********finishservice out********:" + finishService);
			return finishService;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****finishService error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}
		return jObject.toJSONString();
	}

	// 加的方法用于调用
	private String readHttpBody(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		// 读取input流
		// BufferedReader br = new BufferedReader(
		// new InputStreamReader((ServletInputStream) request.getInputStream(),
		// "utf-8"));
		// StringBuffer sb = new StringBuffer("");
		// String temp;
		// while ((temp = br.readLine()) != null) {
		// sb.append(temp);
		// }
		// br.close();
		try {
			return (String) request.getAttribute("json");
		} catch (Exception e) {
			logger.error("*****readHttpBody error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}
		return jObject.toJSONString();
	}

	// 客服下线
	@CrossOrigin
	@RequestMapping(value = "/api2/serverOffline", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服端
	@ResponseBody
	public String serverOffline(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********serverOffline in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int serverId = obj.getInteger("serverId");
			logger.info("smscode userId:{}", serverId);
			String serverOffline = serveronlineservice.serverOffline(serverId);
			logger.info("********serverOffline out********:" + serverOffline);
			return serverOffline;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****serverOffline error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}
		return jObject.toJSONString();
	}

	// 客户 完成咨询
	@CrossOrigin
	@RequestMapping(value = "/api/customer/public/userFinishServer", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String userFinishServer(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********userFinishServer in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int userId = obj.getInteger("userId");
			logger.info("smscode userId:{}", userId);
			String userFinishServer = serveronlineservice.userFinishServer(userId);
			logger.info("********userFinishServer out********:" + userFinishServer);
			return userFinishServer;

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****userFinishServer error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}
		return jObject.toJSONString();
	}

	// 客户 发送消息
	@CrossOrigin
	@RequestMapping(value = "/api/customer/public/userSendMsg", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String userSendMsg(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********userSendMsg in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int srcId = obj.getInteger("srcId");
			int dstId = obj.getInteger("dstId");
			String message = obj.getString("message");
			logger.info("userSendMsg srcId:{},dstId:{},message:{}", srcId, dstId, message);
			String userSendMsg = manualCustomerservice.userSendMsg(srcId, dstId, message);
			logger.info("********userSendMsg out********:" + userSendMsg);
			return userSendMsg;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****userSendMsg error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}
		return jObject.toJSONString();
	}

	// 客服发送 消息
	@CrossOrigin
	@RequestMapping(value = "/api2/serverSendMsg", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服端
	@ResponseBody
	public String serverSendMsg(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********serverSendMsg in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int srcId = obj.getInteger("serverId");
			int dstId = obj.getInteger("dstId");
			String message = obj.getString("message");
			// int srcId = Integer.parseInt(request.getParameter("srcId"));
			// int dstId = Integer.parseInt(request.getParameter("dstId"));
			// String message = request.getParameter("message");
			logger.info("serverSendMsg srcId:{},dstId:{},message:{}", srcId, dstId, message);
			String serverSendMsg = manualCustomerservice.serverSendMsg(srcId, dstId, message);
			logger.info("********serverSendMsg out********:" + serverSendMsg);
			return serverSendMsg;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****serverSendMsg error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");

		}
		return jObject.toJSONString();
	}

	// 用户接受消息
	@CrossOrigin
	@RequestMapping(value = "/api/customer/public/clientGetMsg", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String clientGetMsg(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志int userId
			logger.info("********clientGetMsg in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int userId = obj.getInteger("userId");
			logger.info("smscode userId:{}", userId);
			String clientGetMsg = manualCustomerservice.clientGetMsg(userId);
			logger.info("********clientGetMsg out********:" + clientGetMsg);
			return clientGetMsg;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****clientGetMsg error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");

		}
		return jObject.toJSONString();
	}

	// 客服接受消息
	@CrossOrigin
	@RequestMapping(value = "/api2/serverGetMsg", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服端
	@ResponseBody
	public String serverGetMsg(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志int userId
			logger.info("********serverGetMsg in********:" + body);
			JSONObject obj = JSON.parseObject(body);

			int serverId = obj.getInteger("serverId");
			int userId = obj.getInteger("userId");
			logger.info("serverGetMsg userId:{} userId:{}", serverId, userId);
			String serverGetMsg = manualCustomerservice.serverGetMsg(serverId, userId);
			logger.info("********serverGetMsg out********:" + serverGetMsg);
			return serverGetMsg;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****serverGetMsg error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");

		}
		return jObject.toJSONString();
	}

	// 客服自动接收客户
	@CrossOrigin
	@RequestMapping(value = "/api2/serverAutoGetClient", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服端
	@ResponseBody
	public String serverAutoGetClient(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志int userId
			logger.info("********serverAutoGetClient in********:" + body);
			JSONObject obj = JSON.parseObject(body);

			int serverId = obj.getInteger("serverId");
			int userId = 0;
			logger.info("serverGetMsg userId:{} userId:{}", serverId, userId);
			String serverGetMsg = manualCustomerservice.serverGetMsg(serverId, userId);
			logger.info("********serverAutoGetClient out********:" + serverGetMsg);
			return serverGetMsg;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****serverAutoGetClient error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");

		}
		return jObject.toJSONString();
	}

	// 自助客服问题录入
	@CrossOrigin
	@RequestMapping(value = "/api2/insertquestion", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String insertquestion(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志String question, String answer
			logger.info("********insertquestion in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			String question = obj.getString("question");
			String answer = obj.getString("answer");
			int type = obj.getInteger("type");
			logger.info("smscode question:{},answer:{}", question, answer);
			String insertquestion = automaticcustomerservice.insertquestion(question, answer, type);
			logger.info("********insertquestion out********:" + insertquestion);
			return insertquestion;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****insertquestion error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}
		return jObject.toJSONString();
	}

	// 自助客服问题修改
	@CrossOrigin
	@RequestMapping(value = "/api2/updataquestion", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String updataquestion(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********updataquestion in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int questionId = obj.getInteger("questionId");
			String question = obj.getString("question");
			String answer = obj.getString("answer");
			logger.info("smscode questionId:{},question:{},answer:{}", questionId, question, answer);
			String updataquestion = automaticcustomerservice.updataquestion(questionId, question, answer);
			logger.info("********updataquestion out********:" + updataquestion);
			return updataquestion;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****updataquestion error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}
		return jObject.toJSONString();
	}

	// 自助客服问题删除
	@CrossOrigin
	@RequestMapping(value = "/api2/deletequestion", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String deletequestion(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********deletequestion in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int questionId = obj.getInteger("questionId");
			logger.info("smscode questionId:{}", questionId);
			String deletequestion = automaticcustomerservice.deletequestion(questionId);
			logger.info("********deletequestion out********:" + deletequestion);
			return deletequestion;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****deletequestion error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}

		return jObject.toJSONString();
	}

	// 自助客服问题请求应答
	@CrossOrigin
	@RequestMapping(value = "/api/customer/public/question", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String question(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********question in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int questionId = obj.getInteger("questionId");
			logger.info("smscode questionId:{}", questionId);
			String question = automaticcustomerservice.question(questionId);
			logger.info("********question out********:" + question);
			return question;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****question error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");

		}
		return jObject.toJSONString();
	}

	// 自动 拉取 问题目录
	@CrossOrigin
	@RequestMapping(value = "/api/customer/public/getquestions", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String getquestions(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********getquestions in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			// int userId = obj.getInteger("userId");
			// logger.info("getquestions userId:{}", userId);
			String getquestions = automaticcustomerservice.getquestions();
			logger.info("********getquestions out********:" + getquestions);

			return getquestions;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****getquestions error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");

		}

		return jObject.toJSONString();
	}

	/**
	 * 客服拉取最近30分钟消息
	 */
	@CrossOrigin
	@RequestMapping(value = "/api2/serverGetLastMsg", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服端
	@ResponseBody
	public String serverGetLastMsg(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********serverGetLastMsg in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int serverId = obj.getInteger("serverId");
			logger.info("serverGetLastMsg serverId:{}", serverId);
			String serverGetLastMsg = manualCustomerservice.serverGetLastMsg(serverId);
			logger.info("********serverGetLastMsg out********:" + serverGetLastMsg);
			return serverGetLastMsg;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****serverGetLastMsg error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");

		}
		return jObject.toJSONString();
	}

	/**
	 * 客服拉取历史消息
	 */
	@CrossOrigin
	@RequestMapping(value = "/api2/serverGetHistoryMsg", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服端
	@ResponseBody
	public String serverGetHistoryMsg(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********serverGetHistoryMsg in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int serverId = obj.getInteger("serverId");
			int userId = obj.getInteger("userId") == null ? 0 : obj.getInteger("userId");
			int pageNum = obj.getInteger("pageNum");
			int pageSize = obj.getInteger("pageSize");
			int minMsgId = obj.getInteger("minMsgId");
			logger.info("serverGetLastMsg serverId:{},userId:{},pageNum:{},pageSize:{},minMsgId:{}", serverId, userId,
					pageNum, pageSize, minMsgId);
			String serverGetHistoryMsg = manualCustomerservice.serverGetHistoryMsg(serverId, userId, pageNum, pageSize,
					minMsgId);
			logger.info("********serverGetHistoryMsg out********:" + serverGetHistoryMsg);
			return serverGetHistoryMsg;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****serverGetHistoryMsg error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}
		return jObject.toJSONString();

	}

	/**
	 * 客户端拉取历史消息
	 */
	@CrossOrigin
	@RequestMapping(value = "/api/customer/public/clientGetHistoryMsg", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String clientGetHistoryMsg(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********clientGetHistoryMsg in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int userId = obj.getInteger("userId");
			int pageNum = obj.getInteger("pageNum");
			int pageSize = obj.getInteger("pageSize");
			logger.info("clientGetHistoryMsg userId:{},pageNum:{},pageSize:{}", userId, pageNum, pageSize);
			String clientGetHistoryMsg = manualCustomerservice.clientGetHistoryMsg(userId, pageNum, pageSize);
			logger.info("********clientGetHistoryMsg out********:" + clientGetHistoryMsg);
			return clientGetHistoryMsg;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****clientGetHistoryMsg error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");

		}
		return jObject.toJSONString();
	}

	/*
	 * 客服拉取客户id
	 */
	@CrossOrigin
	@RequestMapping(value = "/api2/servergetclientid", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服端
	@ResponseBody
	public String serverGetClientId(HttpServletRequest request) {

		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********serverGetClientId in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int serverId = obj.getInteger("serverId");
			int clientId = obj.getInteger("clientId");
			logger.info("serverGetClientId serverId:{}, clientId:{}", serverId, clientId);
			String serverGetClientId = manualCustomerservice.serverGetClientId(serverId, clientId);
			logger.info("********serverGetClientId out********:" + serverGetClientId);
			return serverGetClientId;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****serverGetClientId error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");

		}
		return jObject.toJSONString();
	}

	/*
	 * 客服状态设置
	 */
	@CrossOrigin
	@RequestMapping(value = "/api2/setserveridle", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服端
	@ResponseBody
	public String setServerIdle(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********setServerIdle in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int serverId = obj.getInteger("serverId");
			int idle = obj.getInteger("idle");
			logger.info("setServerIdle serverId:{}, idle:{}", serverId, idle);
			String ret = serveronlineservice.setServerIdle(serverId, (byte) idle);
			logger.info("********setServerIdle out********:" + ret);
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****setServerIdle error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}
		return jObject.toJSONString();
	}

	/**
	 * 获取等待客户信息
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@CrossOrigin
	@RequestMapping(value = "/api2/getwaitclientinfo", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服端
	@ResponseBody
	public String getWaitClientInfo(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********getWaitClientInfo in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int serverId = obj.getInteger("serverId");
			logger.info("getWaitClientInfo serverId:{}", serverId);
			String ret = serveronlineservice.getWaitClientInfo(serverId);
			logger.info("********getWaitClientInfo out********:" + ret);
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****getWaitClientInfo error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");

		}
		return jObject.toJSONString();
	}

	/**
	 * 客服登录
	 */
	@CrossOrigin
	@RequestMapping(value = "/api2/serverlogin", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服端
	@ResponseBody
	public String serverlogin(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********serverlogin in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			String userMark = obj.getString("userMark");
			String password = obj.getString("password");
			String deviceId = obj.getString("deviceId");
			logger.info("userMark:{},password:{}", userMark, password);
			String serverLogin = manualCustomerservice.serverLogin(userMark, password, deviceId);
			logger.info("********serverlogin out********:" + serverLogin);
			return serverLogin;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****serverlogin error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");

		}
		return jObject.toJSONString();
	}

	/**
	 * 修改密码
	 */
	@CrossOrigin
	@RequestMapping(value = "/api2/serverModifyPasswd", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服端
	@ResponseBody
	public String serverModifyPasswd(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********serverModifyPasswd in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			Integer userId = obj.getInteger("userId");
			String newPassword = obj.getString("newPassword");
			String oldPassword = obj.getString("oldPassword");
			logger.info("userId:{},newPassword:{},oldPassword:{}", userId, newPassword, oldPassword);
			String serverModifyPasswd = manualCustomerservice.serverModifyPasswd(userId, newPassword, oldPassword);
			logger.info("********serverModifyPasswd out********:" + serverModifyPasswd);
			return serverModifyPasswd;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****serverModifyPasswd error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");

		}
		return jObject.toJSONString();
	}

	/**
	 * 客服修改个人信息
	 */
	@CrossOrigin
	@RequestMapping(value = "/api2/updatecustomerInfo", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服端
	@ResponseBody
	public String updatecustomerInfo(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********updatecustomerInfo in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			Integer serverId = obj.getInteger("serverId");
			String nickName = obj.getString("nickName");
			String phoneNum = obj.getString("phoneNum");
			logger.info("serverId:{},nickName:{},phoneNum:{}", serverId, nickName, phoneNum);
			String updateServer = manualCustomerservice.updatecustomerInfo(serverId, nickName, phoneNum);
			logger.info("********updatecustomerInfo out********:" + updateServer);
			return updateServer;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****updatecustomerInfo error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}
		return jObject.toJSONString();
	}

	/**
	 * 客服提现接口
	 */
	@CrossOrigin
	@RequestMapping(value = "/api2/customercash", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服端
	@ResponseBody
	public String customerCash(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********customerCash in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int serverId = obj.getInteger("serverId");
			String token = obj.getString("token");
			int userId = obj.getInteger("userId");
			int withdrawcash = obj.getInteger("withdrawcash");
			int withdrawDiamond = obj.getInteger("withdrawDiamond");
			logger.info("serverId:{},userId:{},withdrawcash:{},withdrawDiamond:{}", serverId, userId, withdrawcash,
					withdrawDiamond);
			String customerRecharge = manualCustomerservice.customerCash(serverId, token, userId, withdrawcash,
					withdrawDiamond);
			logger.info("********customerCash out********:" + customerRecharge);
			return customerRecharge;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****customerCash error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");

		}
		return jObject.toJSONString();
	}

	/**
	 * 客服充值接口
	 */
	@CrossOrigin
	@RequestMapping(value = "/api2/customerRecharge", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服端
	@ResponseBody
	public String customerRecharge(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********customerRecharge in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int serverId = obj.getInteger("serverId");
			String token = obj.getString("token");
			String orderId = obj.getString("orderId");
			int userId = obj.getInteger("userId");
			int rechargeAmount = obj.getInteger("rechargeAmount");
			int rechargeDiamond = obj.getInteger("rechargeDiamond");
			logger.info("serverId:{},userId:{},rechargeAmount:{},rechargeDiamond:{}", serverId, userId, rechargeAmount,
					rechargeDiamond);
			String customerRecharge = manualCustomerservice.customerRecharge(serverId, token, userId, rechargeAmount,
					rechargeDiamond, orderId);
			logger.info("********customerRecharge out********:" + customerRecharge);
			return customerRecharge;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****customerRecharge error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}
		return jObject.toJSONString();
	}

	/**
	 * 获取彩钻数量接口
	 */
	@CrossOrigin
	@RequestMapping(value = "/api2/getDiamondNum", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" }) // 客服端
	@ResponseBody
	public String getDiamondNum(HttpServletRequest request) {
		JSONObject jObject = new JSONObject();
		try {
			String body = readHttpBody(request);
			// 日志
			logger.info("********getDiamondNum in********:" + body);
			JSONObject obj = JSON.parseObject(body);
			int serverId = obj.getInteger("serverId");
			int rechargeAmount = obj.getInteger("rechargeAmount");
			int flag = obj.getInteger("flag") == null ? 0 : 1;
			logger.info("serverId:{},rechargeAmount:{}, flag:{}", serverId, rechargeAmount, flag);
			String getDiamondNum = manualCustomerservice.getDiamondNum(serverId, rechargeAmount, flag);
			logger.info("********getDiamondNum out********:" + getDiamondNum);
			return getDiamondNum;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("*****getDiamondNum error******:", e);
			jObject.put("result", "1");
			jObject.put("msg", "异常");
		}
		return jObject.toJSONString();
	}

	// 客服心跳包
	@CrossOrigin
	@RequestMapping(value = "/api2/heartbeat", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String serverHeartbeat(HttpServletRequest request) throws IOException {
		String body = readHttpBody(request);
		logger.info("********heartbeat in********:" + body);
		JSONObject obj = JSONObject.parseObject(body);

		int serverId = obj.getInteger("serverId");
		String deviceId = obj.getString("deviceId");
		String heartbeat = manualCustomerservice.heartbeat(serverId, deviceId);
		logger.info("********heartbeat out********:" + heartbeat);
		return heartbeat;
	}

}
